using System;
using System.Net;
using System.Net.Sockets;

namespace rudp
{
	public abstract class BaseCommunication
	{
	/*	public BaseCommunication()
		{		
		}

		public abstract void OnMessagePacketReceived( CommunicationState state, MessagePacket receivedPacket );

		public void OnMessagePacketReceived (IAsyncResult result)
		{
			IPEndPoint ipEndPoint = new IPEndPoint (IPAddress.Any, 0);

			CommunicationState state = (CommunicationState)result.AsyncState;
			byte[] receivedData = state.Socket.EndReceive (result, ref ipEndPoint);

			MessagePacket packet = new MessagePacket ();
			packet.FromByteArray (receivedData);

			state.Packets.Add( packet );

			this.OnMessagePacketReceived( state, packet );
		}

		public void ReceiveMessagePacket (CommunicationState state)
		{
		//	state.NextAction = Action.Receive;
			state.Socket.BeginReceive( new AsyncCallback( this.OnMessagePacketReceived ), state );
		}

		public void OnSend (IAsyncResult result)
		{
			CommunicationState state = (CommunicationState)result.AsyncState;

		/*	if( state.NextAction == Action.Send )
				this.SendNextMessagePacket( state );
			else
				this.ReceiveMessagePacket( state );*/
		}

		/*public void SendNextMessagePacket( CommunicationState state )
		{
			MessagePacket messagePacket = state.Packets[state.Index];
		
			state.Index++;

			// Default the next action to send 
		//	state.NextAction = Action.Send;

			// Mark the last packet to request a receipt
			if( state.Packets.IsLastMessagePacket( messagePacket ) )
			{
			//	state.NextAction = Action.Receive;
				messagePacket.Flags = MessageFlags.Data | MessageFlags.ReceiptRequest;
			}

			this.SendMessagePacket( messagePacket, state );
		}

		public void SendMessagePacket( MessagePacket messagePacket, CommunicationState state )
		{
			byte[] data = messagePacket.ToByteArray();

			state.Socket.BeginSend( data, data.Length, "localhost", 5000, this.OnSend, (state ));
		}
	}*/
}

